import os  
import shutil  

fraction = 1/8
# 列出训练集目录中的所有文件  
images_train_dir = os.getcwd()+'/training_set/images/train'
labels_train_dir = os.getcwd()+'/training_set/labels/train'
images_val_dir = os.getcwd()+'/training_set/images/val'
labels_val_dir = os.getcwd()+'/training_set/labels/val'
print(images_train_dir)
sample_count = 0
for filename in os.listdir(images_train_dir):  
    sample_count = sample_count + 1
    if sample_count%8==1:
        dirname = os.path.splitext(filename)[0]
        print(dirname) 
        shutil.move(os.path.join(images_train_dir, f'{dirname}.png'), os.path.join(images_val_dir, f'{dirname}.png'))
        # 判断子目录中是否有对应的标注文件  
        if os.path.exists(os.path.join(labels_train_dir, f'{dirname}.txt')):   
            # 将训练集中的文件移动到验证集目录中
            shutil.move(os.path.join(labels_train_dir, f'{dirname}.txt'), os.path.join(labels_val_dir, f'{dirname}.txt'))  
            print(f"Moved f'{dirname}.txt' to validation set.")  
        else:  
            print(f"f'{dirname}.txt' not exists.")  


